package com.example.business_oa.controller;


import com.example.business_oa.controller.util.GetDate;
import com.example.business_oa.service.*;
import com.example.comment_oa.dubbo.ChaWuXianYiJinDubbo;
import com.example.comment_oa.dubbo.SalaryZhuBiaoDubbo;
import com.example.comment_oa.pojos.*;

import com.example.comment_oa.pojos.xinzi.*;
import com.example.comment_oa.response.AjaxResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/businessOa")
public class KaoQinController {
    @Resource
    KaoQinService kaoQinService;

    @Resource
    SalaryZhuBiaoService salaryZhuBiaoService;
    @Resource
    ChaGangWeiJinService chaGangWeiJinService;
    @Resource
    ChaBuTieService chaBuTieService;
    @Resource
    ChaJiaBanBeiShuService chaJiaBanBeiShuService;
    @Resource
    ChaChiDaoZaoTuiService chaChiDaoZaoTuiService;
    @Resource
    ChaWuXianYiJinService chaWuXianYiJinService;
    @Resource
    ChaQitaService chaQitaService;
    @Resource
    ChaGongZiDanService chaGongZiDanService;

    Date date=null;

    @GetMapping("/kaoqinqingkuang/{gonghao}/{name}")
    public AjaxResponse selectBumenXiala(@PathVariable("gonghao") String gonghao,@PathVariable("name") String name){

        //这里是为了判断上个月的是不是已经生成了
        GetDate getDate = new GetDate();
        String lastDay = getDate.getLastDay();
        String canshu= "SH"+lastDay;
        List<ShsalaryZhubiao> shsalaryZhubiaos = salaryZhuBiaoService.selectZhuId(canshu);
        boolean empty = shsalaryZhubiaos.isEmpty();

        //日期
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        try {
             date = ft.parse(lastDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }


        //如果已经生成这笔单据了，就是true  ，没有生成就去生成这个单据；
        if(empty){
            //System.err.println(gonghao+name);
            ShsalaryZhubiao shsalaryZhubiao = new ShsalaryZhubiao();
            shsalaryZhubiao.setSrGonghao(gonghao);
            shsalaryZhubiao.setSrPerson(name);
            shsalaryZhubiao.setSrId(canshu);
            String substring = lastDay.substring(0,7);
            shsalaryZhubiao.setSrName(substring+"月薪资");
            salaryZhuBiaoService.insertAll(shsalaryZhubiao);

            //在这里计算 下面的从表
            List<MouthAt> kaoqin = kaoQinService.kaoqin(null);
            //如果这个表是空的  也不行。证明考勤归档表没有生成
            if(kaoqin.isEmpty()){
                return AjaxResponse.dosentexit();
            }else {
                //考勤归档循环开始
                for (MouthAt mouthAt : kaoqin) {
                    System.err.println(mouthAt);
                    //部门Id
                    Integer empBuMen = mouthAt.getUserDeptId();
                    //职务ID
                    Integer empZhiWu = mouthAt.getPostId();
                    // 工号
                    String empGongHao = mouthAt.getUserMat();
                    //部门名称
                    String userDept = mouthAt.getUserDept();
                    //职务名称
                    String postName = mouthAt.getPostName();


                    //这里获得 岗位津贴  这就是一个数字
                    List<JobMakeoffer> jobMakeoffers = chaGangWeiJinService.selectGangweijintie(empBuMen, empZhiWu);
                    Double jmoJobsalary = jobMakeoffers.get(0).getJmoJobsalary();

                    //这里获得基本薪资   如果员工状态是0 基本工资就打八折
                    SysEmployee sysEmployee = chaGangWeiJinService.selectJibenxinzi(empGongHao);
                    Double empSalary =0.0;
                    if(mouthAt.getUserState()==0){
                        empSalary= sysEmployee.getEmpSalary()*0.8;
                    }else {
                        empSalary = sysEmployee.getEmpSalary()+0.0;
                    }

                    //在这里获得公司补贴  这是一个集合  往工资单里添加的时候还要一个sum 和，总值
                    List<JobcorresSubsidy> jobcorresSubsidies = chaBuTieService.selectBuTie(empZhiWu, empBuMen);
                    Double gongsibutie = 0.0;
                    for (JobcorresSubsidy jobcorresSubsidy : jobcorresSubsidies) {
                        //往 基本补贴计算里面添加
                        JibenButiejisuan jibenButiejisuan = new JibenButiejisuan();
                        jibenButiejisuan.setJibenButieid(canshu);
                        jibenButiejisuan.setJibenButiename(jobcorresSubsidy.getJcsSubname());
                        jibenButiejisuan.setJibenButiejine(jobcorresSubsidy.getJcsMoney());
                        gongsibutie+=jobcorresSubsidy.getJcsMoney();
                        jibenButiejisuan.setJibenBumen(userDept);
                        jibenButiejisuan.setJibenBumenid(empBuMen);
                        jibenButiejisuan.setJibenZhiwu(postName);
                        jibenButiejisuan.setJibenZhiwuid(empZhiWu);
                        jibenButiejisuan.setJibenRiqi(date);
                        jibenButiejisuan.setJibenName(mouthAt.getUserName());
                        jibenButiejisuan.setJibenGonghao(empGongHao);
                        chaGangWeiJinService.insertButTieJisuan(jibenButiejisuan);
                    }
                    // 这个人单独一天的薪资
                    double oneDaySalary = empSalary / 21.75;


                    //旷工的天数
                    Integer kg = mouthAt.getKg();
                    //缺卡
                    Integer qk = mouthAt.getQk();
                    //补卡
                    Integer rc = mouthAt.getRc();
                    // 计算旷工，缺卡 补卡之后的钱
//                    double kqb = empSalary - (kg * oneDaySalary) - (qk * oneDaySalary * 0.5) + (rc * oneDaySalary * 0.5);
                    // 计算旷工，缺卡 补卡  应该被扣多少钱
                    double kqb = (kg * oneDaySalary) + (qk * oneDaySalary * 0.5) - (rc * oneDaySalary * 0.5);

                    //节假日加班的天数
                    Integer ovj = mouthAt.getOvj();
                    //周末加班的天数
                    Integer ov = mouthAt.getOv();
                    // 节假日加班要给的钱
                    List<OvertimeBonus> chajiabanbeishu = chaJiaBanBeiShuService.chajiabanbeishu(2);
                    Double beishu = (chajiabanbeishu.isEmpty()) ? 1 : chajiabanbeishu.get(0).getObLimit();
                    Double JieJiaRiJiaBan = beishu * oneDaySalary *ovj;
                    //周末加班要给的钱
                    List<OvertimeBonus> chajiabanbeishu1 = chaJiaBanBeiShuService.chajiabanbeishu(1);
                    Double beishu2 = (chajiabanbeishu1.isEmpty()) ? 1 : chajiabanbeishu1.get(0).getObLimit();
                    Double ZhouMoJiaBan = beishu2 * oneDaySalary * ov;

                    //迟到的次数 早退的次数  迟到是扣钱   1是迟到 2 是早退 3 是全勤
                    Integer late = mouthAt.getLate();
                    Integer early = mouthAt.getEarly();
                    //迟到一次扣得钱
                    List<BelateMoney> belateMonies = chaChiDaoZaoTuiService.selectChiDaoZaoTui(1);
                    Double lateMoney = ((belateMonies.isEmpty()) ? 0 : belateMonies.get(0).getBlMoney()) * late;
                    //早退一次扣的钱
                    List<BelateMoney> belateMonies1 = chaChiDaoZaoTuiService.selectChiDaoZaoTui(2);
                    Double zaotuiMoney = ((belateMonies1.isEmpty()) ? 0 : belateMonies1.get(0).getBlMoney()) * early;


                    //严重迟到，早退的次数  这是扣半天的工资
                    Integer biglate = mouthAt.getBiglate();
                    Integer bigearly = mouthAt.getBigearly();
                    //严重迟到扣的钱
                    Double biglatemoney = biglate * oneDaySalary * 0.5;
                    //严重早退扣的钱
                    Double bigearlymoney = bigearly * oneDaySalary * 0.5;

                    //带薪请假的天数，和请假的天数
                    // 带薪请假不用扣钱，但是没有全勤
                    Integer holiMoney = mouthAt.getHoliMoney();
                    // 请假要扣钱
                    Integer holi = mouthAt.getHoli();
                    Double holiqian = holi * oneDaySalary;

                    //全勤奖的钱
                    //  满足全勤的条件就是 ：应出勤=实际出勤，并且没有迟到早退和严重迟到早退，并且没有缺卡。并且没有请假，也没有带薪请假
                    List<BelateMoney> belateMonies2 = chaChiDaoZaoTuiService.selectChiDaoZaoTui(3);

                    // 应出勤
                    Integer ycq = mouthAt.getYcq();
                    //实际出勤
                    Integer sjcq = mouthAt.getSjcq();
                    //这是算全勤奖的钱
                    Double quanqinjiang = 0.0;
                    if (ycq == sjcq && late == 0 && early == 0 && biglate == 0 && bigearly == 0 && rc == 0 && holi == 0 && holiMoney == 0) {
                        quanqinjiang = (belateMonies2.isEmpty()) ? 0.0 : belateMonies2.get(0).getBlMoney();
                    } else {
                        quanqinjiang = 0.0;
                    }

                    // 五险一金扣款


                    //在这里新增我的数据
                    //这个是基本薪资
                    JibenXinzijisuan jibenXinzijisuan = new JibenXinzijisuan();
                    jibenXinzijisuan.setJibenId(canshu);
                    jibenXinzijisuan.setJibenBumen(mouthAt.getUserDept());
                    jibenXinzijisuan.setJibenName(mouthAt.getUserName());
                    jibenXinzijisuan.setJibenBumenid(mouthAt.getUserDeptId());
                    jibenXinzijisuan.setJibenZhiwu(mouthAt.getPostName());
                    jibenXinzijisuan.setJibenZhiwuid(mouthAt.getPostId());
                    jibenXinzijisuan.setJibenRiqi(date);
                    jibenXinzijisuan.setJibenXinzi(empSalary + 0.0);
                    jibenXinzijisuan.setJibenGangweijintie(jmoJobsalary);
                    jibenXinzijisuan.setJibenGonghao(mouthAt.getUserMat());
                    chaGangWeiJinService.insertall(jibenXinzijisuan);

                    // 这里是计算考勤核算薪资
                    JibenKaoqinhesuan jibenKaoqinhesuan = new JibenKaoqinhesuan();
                    jibenKaoqinhesuan.setJibenId(canshu);
                    jibenKaoqinhesuan.setJibenName(mouthAt.getUserName());
                    jibenKaoqinhesuan.setJibenGonghao(mouthAt.getUserMat());
                    jibenKaoqinhesuan.setJibenBumen(mouthAt.getUserDept());
                    jibenKaoqinhesuan.setJibenBumenid(mouthAt.getUserDeptId());
                    jibenKaoqinhesuan.setJibenZhiwu(mouthAt.getPostName());
                    jibenKaoqinhesuan.setJibenZhiwuid(mouthAt.getPostId());
                    jibenKaoqinhesuan.setJibenRiqi(date);
                    jibenKaoqinhesuan.setJibenYcq(ycq);
                    jibenKaoqinhesuan.setJibenSjcq(sjcq);
                    jibenKaoqinhesuan.setJibenKg(kg);
                    jibenKaoqinhesuan.setJibenQk(qk);
                    jibenKaoqinhesuan.setJibenLate(late);
                    jibenKaoqinhesuan.setJibenBiglate(biglate);
                    jibenKaoqinhesuan.setJibenEarly(early);
                    jibenKaoqinhesuan.setJibenBigearly(bigearly);
                    jibenKaoqinhesuan.setJibenOvj(ovj);
                    jibenKaoqinhesuan.setJibenOv(ov);
                    jibenKaoqinhesuan.setJibenHoli(holi);
                    jibenKaoqinhesuan.setJibenHolimoney(holiMoney);
                    jibenKaoqinhesuan.setJibenBusi(mouthAt.getBusi());
                    jibenKaoqinhesuan.setJibenRc(rc);
                    jibenKaoqinhesuan.setJibenChuqinkoukuan(kqb);
                    jibenKaoqinhesuan.setJibenChidaokoukuan(lateMoney );
                    jibenKaoqinhesuan.setJibenZaotuikoukuan(zaotuiMoney );
                    jibenKaoqinhesuan.setJibenYzchidaokoukuan(biglatemoney);
                    jibenKaoqinhesuan.setJibenYzzaotuikoukuan(bigearlymoney);
                    jibenKaoqinhesuan.setJibenJiejairijiaban(JieJiaRiJiaBan);
                    jibenKaoqinhesuan.setJibenZhoumojiaban(ZhouMoJiaBan);
                    jibenKaoqinhesuan.setJibenQingjia(holiqian);
                    jibenKaoqinhesuan.setJibenQuanqinjiang(quanqinjiang);
                    jibenKaoqinhesuan.setJibenZonghe(kqb+lateMoney+zaotuiMoney+biglatemoney+bigearlymoney-JieJiaRiJiaBan-ZhouMoJiaBan+holiqian-quanqinjiang);
                    chaGangWeiJinService.insertKaoQinHeSuan(jibenKaoqinhesuan);

                    //这里是查询五险一金 缴纳
                    Double wuxianyijinzonghe = 0.0;
                    List<FiveGold> selectwuxainyijin = chaWuXianYiJinService.selectwuxainyijin();
                    Double yanglaobaoxian =selectwuxainyijin.get(0).getFxCmopare() * empSalary;
                    Double yiliaobaoxian =selectwuxainyijin.get(1).getFxCmopare() * empSalary;
                    Double shiyebaoxain = selectwuxainyijin.get(2).getFxCmopare() * empSalary;
                    Double gongshangbaoxain = selectwuxainyijin.get(3).getFxCmopare() * empSalary;
                    Double shengyubaoxian = selectwuxainyijin.get(4).getFxCmopare() * empSalary;
                    Double gongjijinbaoxian = selectwuxainyijin.get(5).getFxCmopare() * empSalary;
//                    System.err.println("工号"+empGongHao+",基本薪资"+empSalary+"养老保险："+yanglaobaoxian+","+yiliaobaoxian+","+shiyebaoxain+","+gongshangbaoxain
//                            +","+shengyubaoxian+","+gongjijinbaoxian);
                    wuxianyijinzonghe = yanglaobaoxian+yiliaobaoxian+shiyebaoxain+gongshangbaoxain+shengyubaoxian+gongjijinbaoxian;
                    JibenWuxianyijin jibenWuxianyijin = new JibenWuxianyijin();
                    jibenWuxianyijin.setJibenId(canshu);
                    jibenWuxianyijin.setJibenName(mouthAt.getUserName());
                    jibenWuxianyijin.setJibenBumen(userDept);
                    jibenWuxianyijin.setJibenBumenid(empBuMen);
                    jibenWuxianyijin.setJibenGonghao(empGongHao);
                    jibenWuxianyijin.setJibenZhiwu(postName);
                    jibenWuxianyijin.setJibenZhiwuid(empZhiWu);
                    jibenWuxianyijin.setJibenYanglaobaoxian(yanglaobaoxian);
                    jibenWuxianyijin.setJibenYiliaobaoxain(yiliaobaoxian);
                    jibenWuxianyijin.setJibenShiyebaoxain(shiyebaoxain);
                    jibenWuxianyijin.setJibenGongshangbaoxian(gongshangbaoxain);
                    jibenWuxianyijin.setJibenShengyubaoxian(shengyubaoxian);
                    jibenWuxianyijin.setJibenGongjijinbaoxian(gongjijinbaoxian);
                    jibenWuxianyijin.setJibenZonghe(yanglaobaoxian+yiliaobaoxian+shiyebaoxain+gongshangbaoxain+shengyubaoxian+gongjijinbaoxian);
                    chaWuXianYiJinService.tianjaiwuxianyijin(jibenWuxianyijin);

                    //其他事件添加进去
                    JibenQitajine jibenQitajine = new JibenQitajine();
                    jibenQitajine.setJibenId(canshu);
                    jibenQitajine.setJibenName(mouthAt.getUserName());
                    jibenQitajine.setJibenBumen(userDept);
                    jibenQitajine.setJibenBumenid(empBuMen);
                    jibenQitajine.setJibenZhiwu(postName);
                    jibenQitajine.setJibenZhiwuid(empZhiWu);
                    jibenQitajine.setJibenGonghao(empGongHao);
                    jibenQitajine.setJibenShijianjianshu("无");
                    jibenQitajine.setJibenJine(0.0);
                    chaQitaService.insertQita(jibenQitajine);

                    List<JibenQitajine> jibenQitajines = chaQitaService.selectOnePerson(canshu, empGongHao);
                    //每一个人的这个其他扣款金额
                    Double qitaKouKuanjine = 0.0;
                    for (JibenQitajine qitajine : jibenQitajines) {
                        qitaKouKuanjine = qitaKouKuanjine + qitajine.getJibenJine();
                    }

                    //新增工资单
                    JibenGongzidan jibenGongzidan = new JibenGongzidan();
                    jibenGongzidan.setJibenId(canshu);
                    jibenGongzidan.setJibenName(mouthAt.getUserName());
                    jibenGongzidan.setJibenGonghao(empGongHao);
                    jibenGongzidan.setJibenBumen(userDept);
                    jibenGongzidan.setJibenBumenid(empBuMen);
                    jibenGongzidan.setJibenZhiwu(postName);
                    jibenGongzidan.setJibenZhiwuid(empZhiWu);
                    jibenGongzidan.setJibenJibenxinzi(empSalary);
                    jibenGongzidan.setJibenGangweijintie(jmoJobsalary);
                    jibenGongzidan.setJibenKaoqinxinzi(kqb+lateMoney+zaotuiMoney+biglatemoney+bigearlymoney-JieJiaRiJiaBan-ZhouMoJiaBan+holiqian-quanqinjiang);
                    jibenGongzidan.setJibenJiabanxinzi(0.0);
                    jibenGongzidan.setJibenWuxianyijindaikou(wuxianyijinzonghe);
                    jibenGongzidan.setJibenGongsibutie(gongsibutie);
                    jibenGongzidan.setJibenQita(qitaKouKuanjine);
                    jibenGongzidan.setJibenShifagongzi(empSalary+jmoJobsalary
                            -(kqb+lateMoney+zaotuiMoney+biglatemoney+bigearlymoney-JieJiaRiJiaBan-ZhouMoJiaBan+holiqian-quanqinjiang)
                            -wuxianyijinzonghe+gongsibutie+qitaKouKuanjine);
                    jibenGongzidan.setJibenBeizhu(substring+"月薪资");
                    chaGongZiDanService.insertAllGongZiDan(jibenGongzidan);

                }
            }
        //考勤归档循环结束
            return AjaxResponse.success();
        }else{
            return  AjaxResponse.dosentexit();
        }


//        List<MouthAt> kaoqin = kaoQinService.kaoqin(null);
//        for (MouthAt mouthAt : kaoqin) {
//            System.out.println(mouthAt);
//        }
//        System.err.println(kaoqin);

    }
}
